package com.kun.controller;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kun.common.Constants;
import com.kun.common.Result;
import com.kun.controller.dto.RoleMenuDto;
import com.kun.entity.Menu;
import com.kun.entity.Role;
import com.kun.entity.RoleMenu;
import com.kun.exception.MyServiceException;
import com.kun.mapper.MenuMapper;
import com.kun.service.RoleMenuServiceImpl;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {


    @Autowired
    private RoleMenuServiceImpl roleMenuService;


    @Autowired
    private MenuMapper menuMapper;


    @GetMapping("/selectAllMenus/{roleId}")
    @ApiOperation("查询该角色所有的菜单权限")
    public Result selectAllMenus(@PathVariable("roleId")Integer roleId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("roleId",roleId);
        List<RoleMenu> roleMenus = roleMenuService.selectAllRoleMenus(map);
        ArrayList<Menu> menus = new ArrayList<>();
        for (RoleMenu roleMenu : roleMenus) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id",roleMenu.getMenuId());
            List<Menu> list = menuMapper.selectAllMenus(map1);
            if (!StringUtils.isEmpty(list.get(0).getPath())){
                menus.add(list.get(0));
            }
        }
        return Result.success(menus);
    }

    @GetMapping("/deleteAllMenus/{roleId}")
    @ApiOperation("删除该角色所有的菜单权限")
    public Result deleteAllMenus(@PathVariable("roleId")Integer roleId){
        return roleMenuService.deleteRoleMenu(roleId);
    }


    @PostMapping("/addAllMenus")
    @ApiOperation("增加选中的所有菜单权限")
    public Result addAllMenus(@RequestBody RoleMenuDto roleMenuDto){
        ArrayList<Integer> parentMenuIds = new ArrayList<>();
        for (Integer menuId : roleMenuDto.getIds()) {
            RoleMenu roleMenu = new RoleMenu(roleMenuDto.getRoleId(), menuId);
//            1.通过menuId查询到menu
            HashMap<String, Object> menuMap = new HashMap<>();
            menuMap.put("id",menuId);
            List<Menu> menus = menuMapper.selectAllMenus(menuMap);
            if (menus.size()!=1){
                throw new MyServiceException(Constants.CODE_600,"menu数据库异常,存在id相同的数据");
            }
//            2.判断menu的parentId是否存在,若存在,则存储下来
            if(menus.get(0).getParentId()!=null){
                parentMenuIds.add(menus.get(0).getParentId());
            }
            roleMenuService.addRoleMenu(roleMenu);
        }
//        3.将存储下来的parentId依次添加到roleMenu关系表中（如果数据库中未存在）
        for (Integer parentMenuId : parentMenuIds) {
            HashMap<String, Object> roleMenusMap = new HashMap<>();
            roleMenusMap.put("roleId",roleMenuDto.getRoleId());
            roleMenusMap.put("menuId",parentMenuId);
            if (roleMenuService.selectAllRoleMenus(roleMenusMap).size()==0){
                roleMenuService.addRoleMenu(new RoleMenu(roleMenuDto.getRoleId(),parentMenuId) );
            }
        }
        return Result.success();
    }




}
